//@author A0101906R
#include "Cmd.h"

using namespace Twentyfifth;
using namespace std;

const std::string Cmd::FEEDBACK_ERR_VOID = "Error: void\n";
const std::string Cmd::FEEDBACK_ERR_END_OF_FILE = "Error: end of file reached.\n";
const std::string Cmd::FEEDBACK_ERR_INVALID_COMMAND = "Error: invalid command.\n";
const std::string Cmd::FEEDBACK_ERR_TASK_NOT_FOUND = "Error: task not found.\n";
const std::string Cmd::FEEDBACK_ERR_NO_RECORDS = "Error: no record.\n";
const std::string Cmd::FEEDBACK_ERR_NO_RESULT = "Error: no result.\n";
const std::string Cmd::FEEDBACK_EXIT = "exit\n";
const std::string Cmd::FEEDBACK_OK = "OK\n";
const std::string Cmd::ERROR_UNSUCCESSFUL = "Previous action was unsuccessful, cannot undo.\n";
//@author A0101906R
Cmd::Cmd() {
	cmdType = INVALID;
}
//@author A0101906R
string Cmd::toString() {
	return task.toString();
}
//@author A0101906R
void Cmd::print() {
	task.print();
}
//@author A0101906R
void Cmd::setStorageMgr(StorageMgr* instance) {
	storage = instance;
}

Cmd::CmdType Cmd::identifyCmdType(string s) {
	if (s == "add") {
		return ADD;
	} else if (s == "edit") {
		return EDIT;
	} else if (s == "view") {
		return VIEW;
	} else if (s == "search") {
		return SEARCH;
	} else if (s == "delete") {
		return DEL;
	} else if (s == "undo") {
		return UNDO;
	} else if (s == "exit") {
		return EXIT;
	} else {
		return INVALID;
	}
}
//@author A0101906R
void Cmd::setCmdType(Cmd::CmdType ct) {
	cmdType = ct;
}
//@author A0101906R
void Cmd::setTask(Task newTask) {
	task = newTask;
}
//@author A0101906R
Cmd::CmdType Cmd::getCmdType() {
	return cmdType;
}
//@author A0101906R
Task Cmd::getTask() {
	return task;
}
//@author A0101906R
Status Cmd::execution() {
	Status statusVoid;
	return statusVoid;
}
//@author A0101906R
Status Cmd::undo(Status::StatusCode c) {
	Status statusVoid;
	statusVoid.code = Status::ERR_VOID;
	statusVoid.desc = FEEDBACK_ERR_VOID;

	return statusVoid;
}
//@author A0101906R
std::string Cmd::statusCodeToString(Status status) {
	string s;
	switch (status.code) {
	case Status::ERR_VOID:
		s = FEEDBACK_ERR_VOID;
		break;
	case Status::ERR_END_OF_FILE:
		s = FEEDBACK_ERR_END_OF_FILE;
		break;
	case Status::ERR_INVALID_COMMAND:
		s = FEEDBACK_ERR_INVALID_COMMAND;
		break;
	case Status::ERR_NOT_FOUND:
		s = FEEDBACK_ERR_TASK_NOT_FOUND;
		break;
	case Status::ERR_NO_RECORDS:
		s = FEEDBACK_ERR_NO_RECORDS;
		break;
	case Status::ERR_NO_RESULT:
		s = FEEDBACK_ERR_NO_RESULT;
		break;
	case Status::ERR_OTHER:
		s = status.desc;
		break;
	case Status::EXIT:
		s = FEEDBACK_EXIT;
	default:
		s = FEEDBACK_OK;
		break;
	}
	return s;

}

DateTime Cmd::getCurrentDateTime() {
	DateTime dt;
	struct tm newtime;
	__time64_t long_time;
	errno_t err;
	_time64(&long_time);
	err = _localtime64_s(&newtime, &long_time);
	if (err) {
		cerr << "ERROR: cannot get current date/time in DbMgr." << endl;
	} else {
		dt.setDay(newtime.tm_mday);
		dt.setMonth(newtime.tm_mon + 1);
		dt.setYear(newtime.tm_year + 1900);
		dt.setHour(newtime.tm_hour);
		dt.setMinute(newtime.tm_min);
		dt.setSecond(newtime.tm_sec);
	}
	return dt;
}
